﻿using ITPaloozaPCL.Interfaces;
using ITPaloozaPlatformSpecific.Desktop;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITPaloozaPlatformSpecific.DesktopApp.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        #region Fields
        private StockManager _stockManager = new StockManager();
        #endregion

        #region Properties
        private ObservableCollection<IStock> _PriceList;
        public ObservableCollection<IStock> PriceList { get { return _PriceList; } }
        #endregion

        #region Ctors
        public MainViewModel()
        {
            _PriceList = new ObservableCollection<IStock>();
            InitializeDataContext();
        }
        #endregion

        #region Public
        #endregion

        #region Private
        private void InitializeDataContext()
        {
            Task<List<IStock>> init = new Task<List<IStock>>(() =>
            {

                List<IStock> stockList = new List<IStock>();
                _stockManager = new StockManager();
                _stockManager.GetData(ref stockList);
                return stockList;
            });

            init.ContinueWith((antecendent) =>
            {
                PriceList.Clear();
                foreach (var stockPrice in antecendent.Result)
                {
                    PriceList.Add(stockPrice);
                }
            }, TaskContinuationOptions.None);

            init.Start();
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

    }
}
